|
|
 |
|
|
|
|
14: I'll take the blame for this error
15: ' --- Unanticipated errors from foreign
16: 'components. ---
17: Case vbObjectError To (vbObjectError + 65536)
18: Err.Raise TRANSLATED_NUMBER + vbObjectError, _
19: CMP_SOURCENAME, LoadResString (ERR_SPN_FAILURE)
20: End Select
21: End Sub |
|
|
|
|
|
|
|
|
Notice the use of the maximum 65535. Some older components may return error numbers in the range 0-65535. |
|
|
|
|
|
|
|
|
In the preceding example a global constant, the string constant COMPONENT_SOURCENAME, is used for the source argument of the Err object's Raise method. If you raise errors anywhere outside your error handler and neglect to specify the source argument, Visual Basic uses your component's Project Name, combined with the Name property of the class. |
|
|
|
|
|
|
|
|
Raising Errors and Exceptions |
|
|
|
|
|
|
|
|
Raising an error involves notifying a client that some systemic error occurred. Raising an exception, while technically the same as raising an error in Visual Basic, is a violation of some policy of which your users inform you. For business applications, the policy violations are enforced by business rules. All policy violations that trigger you to raise an exception should be documented in your use cases, sequence diagrams, and collaboration diagrams. |
|
|
|
|
|
|
|
|
As mentioned earlier, raising errors and exceptions involves invoking the Raise method of the Err object. As Visual Basic's Online Book advises, when you use the Raise method to generate an error, leave the Err object's other properties unchanged. That way, if the raised error is not easily trapped, you can still display the Source and Description properties to help users take their own measures to resolve the error or exception. |
|
|
|
|
|
|
|
|
If you find it difficult to tell which class generated which error, activate the Break in Class Module option. To activate it, choose Tools|Options from the menu. You should see the Options dialog box shown in Figure 18.1. Click the General tab and click the Break in Class Module option button. The Break in Class Module option will cause a class module or an object in another application to enter the debugger's Break mode. At this point, you can analyze the error. As expected, any error that originates in a compiled object will not display in the Immediate window while in Break mode. That compiled object will handle its errors by its error handler or will be subsequently trapped by the referencing module. |
|
|
|
|
|